package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.MyAJAX;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/employee")
public class EmployeeController {


    private IEmployeeService employeeService;
    private IDepartmentService departmentService;
    private IRoleService roleService;

    @Autowired
    public void setRoleService(IRoleService roleService) {
        this.roleService = roleService;
    }

    @Autowired
    public void setEmployeeService(IEmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    @Autowired
    public void setDepartmentService(IDepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    @RequestMapping("/list")
    @RequiredPermission(name="员工页面",expression="employee:list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){
        PageInfo<Employee> pageResult = employeeService.query(qo);
        model.addAttribute("PageInfo",pageResult);
        model.addAttribute("departments",departmentService.selectAll());
        return "/employee/list";
    }
    @RequiredPermission(name="员工删除",expression="employee:delete")
    @RequestMapping("/delete")
    public String delete(Long id ){
        employeeService.delete(id);
        return "redirect:/employee/list";
    }

    //去到新增页面
    @RequiredPermission(name="员工新增/编辑",expression="employee:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id ,Model model ){
        //把部门数据查出来
        model.addAttribute("role",roleService.selectAll());
        model.addAttribute("departments",departmentService.selectAll());
        if(id != null){
            model.addAttribute("roleSelected",roleService.selectEmpAndRole(id));
            model.addAttribute("employee",employeeService.selectByPrimaryKey(id));
        }
        return  "/employee/input";
    }
    @RequiredPermission(name="员工新增/编辑",expression="employee:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public MyAJAX saveOrUpdate(Employee employee, Long[] ids){
        if(employee.getId() != null){
            employeeService.update(employee,ids);
        }else {
            employeeService.insert(employee,ids);
        }
        return new MyAJAX();
    }

    @RequestMapping("/checkName")
    @ResponseBody
    public Map checkName(String username){
        Map<String, Boolean> stringBooleanMap = new HashMap<>();
        Employee employee = employeeService.checkName(username);
        stringBooleanMap.put("valid",employee==null);
        return  stringBooleanMap;
    }

    //文件导出
    @RequiredPermission(name = "员工导出",expression ="employee:exportXls")
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse resp) throws IOException {
        //设置头信息,让下载的文件有名字
        resp.setHeader("Content-Disposition","attachment;filename=employee.xls");
        Workbook  workbook=employeeService.exportXls();
        workbook.write(resp.getOutputStream());
    }
    //文件导入 /employee/importXls
    @RequiredPermission(name = "员工导出",expression ="employee:importXls")
    @RequestMapping("/importXls")
    public String importXls(MultipartFile file,Model model) throws IOException {
        //进行读取并写入数据库操作
        List<String> exceptions = employeeService.importXls(file);
        model.addAttribute("exceptions",exceptions);
        return "/employee/Import";
    }
    //重置密码
    @RequiredPermission(name = "员工密码重置",expression ="employee:repeat")
    @RequestMapping("/repeat")
    public String repeat(Long id){
        employeeService.repeatPassword(id);

        return "redirect:/employee/list";
    }
    //改变员工状态
    @RequestMapping("/alterStatus")
    public String alterStatus(Employee employee){
        employeeService.alterStatus(employee);
        return "redirect:/employee/list";
    }

}
